package sf.jooq.mapper;

import org.jooq.Select;
import sf.common.wrapper.Page;
import sf.core.DBObject;
import sf.database.dao.DBClient;
import sf.database.dao.DBContext;
import sf.database.dialect.DBDialect;
import sf.jooq.JooqTables;
import sf.jooq.tables.JooqTable;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * @param <T>
 */
public class JooqMapperImpl<T extends DBObject> implements JooqMapper<T> {
    protected DBClient client;
    protected Class<T> domainClass;

    public JooqMapperImpl(DBClient client, Class<T> domainClass) {
        this.client = client;
        this.domainClass = domainClass;
    }

    @Override
    public JooqTable<?> jooqTable() {
        DBDialect dialect = client.getDialect(false);
        return JooqTables.getTable(null,domainClass, dialect);
    }

    @Override
    public JooqTable<?> jooqTable(DBContext context) {
        DBDialect dialect = client.getDialect(false);
        return JooqTables.getTable(context,domainClass, dialect);
    }

    @Override
    public T jooqSelectOne(Select<?> select) {
        return client.getJooq().jooqSelectOne(select, domainClass);
    }

    @Override
    public <S> S jooqSelectOne(Select<?> select, Class<S> clz) {
        return client.getJooq().jooqSelectOne(select, clz);
    }

    @Override
    public List<T> jooqSelectList(Select<?> select) {
        return client.getJooq().jooqSelectList(select, domainClass);
    }

    @Override
    public Page<T> jooqSelectPage(Select<?> countSelect, Select<?> pageSelect) {
        return client.getJooq().jooqSelectPage(countSelect, pageSelect, domainClass);
    }

    @Override
    public void jooqSelectIterator(Consumer<Iterable<T>> ormIt, Select<?> select) {
        client.getJooq().jooqSelectIterator(ormIt, domainClass, select);
    }

    @Override
    public void jooqSelectStream(Consumer<Stream<T>> ormStream, Select<?> select) {
        client.getJooq().jooqSelectStream(ormStream, domainClass, select);
    }
}
